﻿package org.nickro.project.utils{

    public class Types {

        public function Types(){
            throw (new Error("ERROR org.nickro.project.utils.Types constructor: Class never intended to be instantiated"));
        }
        public static function parseArray(str:String, innerType:String="String", delimiter:String=","):Array{
            var array:Array = [];
            if ((((str == null)) || ((str == "")))){
                return (array);
            };
            var elementList:Array = str.split(delimiter);
            var elements:int = elementList.length;
            var i:int;
            while (i < elements) {
                array[i] = stringToType(elementList[i], innerType);
                i++;
            };
            return (array);
        }
        public static function XMLListToString(xmlList:XMLList, separator:String=","):String{
            var item:XML;
            var result:String = "";
            for each (item in xmlList) {
                result = (result + (item.toString() + separator));
            };
            return (result.substring(0, (result.length - separator.length)));
        }
        public static function XMLListToArray(xmlList:XMLList, toType:String=null, innerType:String=null):Array{
            var item:XML;
            var data:*;
            var result:Array = [];
            if ((toType is String)){
                for each (item in xmlList) {
                    data = Types.stringToType(item.toString(), toType, innerType);
                    result.push(data);
                };
            } else {
                for each (item in xmlList) {
                    result.push(item);
                };
            };
            return (result);
        }
        public static function parseBoolean(val):Boolean{
            if ((((val is XML)) || ((val is XMLList)))){
                val = val.toString();
            };
            var isString = (val is String);
            if (isString){
                val = String(val).toLowerCase();
            };
            if (((isString) && ((((((((val == "0")) || ((val == "no")))) || ((val == "false")))) || (Strings.isWhiteSpace(val)))))){
                return (false);
            };
            return (Boolean(val));
        }
        public static function stringToType(str:String, dataType:String, innerType:String="String"){
            switch (dataType.toLowerCase()){
                case "string":
                    return (str);
                case "int":
                    return (parseInt(str));
                case "uint":
                    return (uint(Math.abs(parseInt(str))));
                case "number":
                    return (parseFloat(str));
                case "boolean":
                    return (parseBoolean(str));
                case "array":
                    return (parseArray(str, innerType));
                case "xml":
                    return (new XML(str));
                default:
                    trace((((("/error/ Unknown dataType in Types.stringToType('" + str) + "','") + dataType) + "')"));
                    return (str);
            };
        }

    }
}
